package cn.szbo.jksystem.core;

import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

/**
 * @author nick xue
 * 
 *         2012-7-20 下午3:46:12
 */
@Repository
public class GenericDaoImpl<T extends Object> implements GenericDao<T> {

	private JdbcTemplate jdbcTemplate;

	@Autowired
	public void setDataSource(DataSource dataSource) {
		this.jdbcTemplate = new JdbcTemplate(dataSource);
	}

	public int insert(String sql, List<Object> values) {
		if (values == null) {
			return this.jdbcTemplate.update(sql);
		} else {
			Object[] obj = values.toArray();
			return this.jdbcTemplate.update(sql, obj);
		}
	}

	public int update(String sql, List<Object> values) {
		if (values == null) {
			return this.jdbcTemplate.update(sql);
		} else {
			Object[] obj = values.toArray();
			return this.jdbcTemplate.update(sql, obj);
		}
	}

	public int delete(String sql, List<Object> values) {
		if (values == null) {
			return this.jdbcTemplate.update(sql);
		} else {
			Object[] obj = values.toArray();
			return this.jdbcTemplate.update(sql, obj);
		}
	}

	public long queryForLong(String sql, List<Object> values) {
		long count = 0;
		if (values == null) {
			count = this.jdbcTemplate.queryForLong(sql);
		} else {
			Object[] obj = values.toArray();
			count = this.jdbcTemplate.queryForLong(sql, obj);
		}
		return count;
	}

	public List<Map<String, Object>> queryForList(String sql,
			List<Object> values) {
		List<Map<String, Object>> rows = null;
		if (values == null) {
			rows = this.jdbcTemplate.queryForList(sql);
		} else {
			Object[] obj = values.toArray();
			rows = this.jdbcTemplate.queryForList(sql, obj);
		}
		return rows;
	}

	/**
	 * 
	 * 
	 * @param sql
	 * @param values
	 * @param userObject
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List queryForList(String sql, List<Object> values,
			RowMapper userObject) {
		List rows;
		if (values == null) {
			rows = this.jdbcTemplate.query(sql, userObject);
		} else {
			Object[] obj = values.toArray();
			rows = this.jdbcTemplate.query(sql, obj, userObject);
		}
		return rows;
	}

	public Map<String, Object> queryForSingle(String sql, List<Object> values) {
		List<Map<String, Object>> row = queryForList(sql, values);
		if (row != null && row.size() > 0) {
			return row.get(0);
		} else {
			return null;
		}
	}

	/**
	 * 
	 * 
	 * @param sql
	 * @param values
	 * @param userObject
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public Object queryForSingle(String sql, List<Object> values,
			RowMapper userObject) {
		List row = queryForList(sql, values, userObject);
		if (row != null && row.size() > 0) {
			return row.get(0);
		} else {
			return null;
		}
	}

}
